﻿
using Xacml.Core;
using Xacml.Core.Policy;
using System;
using System.Collections;
using System.Xml;

namespace Xacml.Core.Context
{
  public class ResponseElement : XacmlElement
  {
    private ResultCollection _results;

    public ResultCollection Results
    {
      get
      {
        return this._results;
      }
    }

    public override bool IsReadOnly
    {
      get
      {
        return true;
      }
    }

    public ResponseElement(ResultElement[] results, XacmlVersion schemaVersion)
      : base(XacmlSchema.Context, schemaVersion)
    {
      this._results = new ResultCollection();
      if (results == null)
        return;
      foreach (ResultElement resultElement in results)
        this._results.Add(resultElement);
    }

    public ResponseElement(XmlReader reader, XacmlVersion schemaVersion)
      : base(XacmlSchema.Context, schemaVersion)
    {
      this._results = new ResultCollection();
      if (reader.LocalName == "Response")
      {
        while (reader.Read())
        {
          switch (reader.LocalName)
          {
            case "Result":
              this._results.Add(new ResultElement(reader, schemaVersion));
              break;
          }
          if (reader.LocalName == "Response" && reader.NodeType == XmlNodeType.EndElement)
            break;
        }
      }
      else
        throw new Exception(Resource.ResourceManager[Resource.MessageKey.exc_invalid_node_name, new object[1]
        {
          (object) reader.LocalName
        }]);
    }

    public void WriteDocument(XmlWriter writer)
    {
      writer.WriteStartDocument();
      writer.WriteStartElement("Response", "urn:oasis:names:tc:xacml:1.0:context");
      foreach (ResultElement resultElement in (CollectionBase) this.Results)
      {
        writer.WriteStartElement("Result", "urn:oasis:names:tc:xacml:1.0:context");
        writer.WriteElementString("Decision", resultElement.Decision.ToString());
        writer.WriteEndElement();
        writer.WriteStartElement("Status");
        writer.WriteStartElement("StatusCode");
        writer.WriteAttributeString("Value", resultElement.Status.StatusCode.Value);
        writer.WriteEndElement();
        writer.WriteEndElement();
        if (resultElement.Obligations != null && resultElement.Obligations.Count != 0)
        {
          writer.WriteStartElement("Obligations");
          foreach (ObligationElement obligationElement in (CollectionBase) resultElement.Obligations)
          {
            writer.WriteStartElement("Obligation");
            writer.WriteAttributeString("ObligationId", obligationElement.ObligationId);
            writer.WriteAttributeString("FulfillOn", obligationElement.FulfillOn.ToString());
            if (obligationElement.AttributeAssignment != null && obligationElement.AttributeAssignment.Count != 0)
            {
              foreach (AttributeAssignmentElement assignmentElement in (CollectionBase) obligationElement.AttributeAssignment)
              {
                writer.WriteStartElement("AttributeAssignment");
                writer.WriteAttributeString("AttributeId", assignmentElement.AttributeId);
                writer.WriteAttributeString("DataType", assignmentElement.DataTypeValue);
                writer.WriteString(assignmentElement.Value);
                writer.WriteEndElement();
              }
            }
            writer.WriteEndElement();
          }
          writer.WriteEndElement();
        }
      }
      writer.WriteEndElement();
      writer.WriteEndDocument();
    }
  }
}
